<template>
  <div class="app-container">
    <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="750px" @close="resetForm('ruleForm')">
      <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
      <div class="formTopFont">库房库位信息</div>
      <el-row>
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px">
          <el-form-item label="上级分类" prop="parentId">
            <tree-select ref="treeSelect" style="width: 100%" :props="{ value: 'id', label: 'wmsName', children: 'children' }" :options="treeList" clearable :disabled="false" :accordion="true" :value="ruleForm.parentId" :search-key="'wmsName'" @getNode="getValue($event)" />
          </el-form-item>
          <el-form-item label="仓库名称" prop="wmsName">
            <el-input v-model="ruleForm.wmsName" maxlength="20" show-word-limit />
          </el-form-item>
          <el-form-item label="仓库编码" prop="wmsCode">
            <el-input v-model="ruleForm.wmsCode" maxlength="20" show-word-limit />
          </el-form-item>
          <el-form-item label="描述" prop="remark">
            <el-input v-model="ruleForm.remark" maxlength="200" type="textarea" rows="3" show-word-limit />
          </el-form-item>
        </el-form>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import TreeSelect from '@/components/TreeSelect'
export default {
  components: { TreeSelect },
  data() {
    const checkName = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/wms/verifyNameUnique?name=${value}&id=${this.ruleForm.id}`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('分类名称重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      ruleForm: {
        wmsName: '',
        wmsCode: '',
        remark: '',
        parentId: '0',
        parentName: '',
        id: ''
      },
      rules: {
        parentId: [{ required: false, message: '请选择上级分类' }],
        wmsName: [
          { required: true, message: '分类名称不能为空！', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      treeList: []
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    showDialog(row, data) {
      console.log(data)
      this.dialogVisible = true
      this.treeList = [{ id: '0', wmsName: '无' }, ...data]
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
    },
    /**
     * @description: 获取选中的上级部门
     * @param {*} value
     * @return {*}
     */
    getValue(value) {
      this.ruleForm.parentId = value.id
      this.ruleForm.parentName = value.wmsName
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.$axios.post('/wms/submit', this.ruleForm)
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        wmsName: '',
        wmsCode: '',
        remark: '',
        parentId: '0',
        parentName: '',
        id: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
